<?php
 global  $arrConfig,$sCanEdit,$lang;
  $sCanEdit = 1;  
  $formTitle = 'quan ly tai khoan'; 
  # start region process data 
initPage();
$objXajax->registerFunction('ajxLoadDataUser');
$objXajax->registerFunction('loadPower');
$objXajax->registerFunction('SaveUser');
$tbl ='tbl_user';
function initPage(){
  global $cmbPowerTypeForSeach,$cmbPowerTypeForAdd,$cmbCityForSearch,$cmbCityForAdd,$objTemplate;    
  $arrPowerType   = getPowerType(false);
  $arrCity = SelectDB('tbl_city') ;
  $objTemplate->MergeBlock('listpower', $arrPowerType);
  $objTemplate->MergeBlock('listcity', $arrCity);
}
  
  function getPowerType($all=true){
      global $objDbSelect,$lang,$objTemplate;
      $power_type = $_SESSION['current']['powertype'];
      $power_type=1;
      $sql = " select power_type_id as id, name_$lang as name from tbl_power_type where power_type_id IN 
      (select power_type_sub from tbl_power_create_user where power_type_main=$power_type and flg=1) and active=1 "; 
      $arrPowerType = $objDbSelect->GetArray($sql);
      return $arrPowerType;     
  }
  # end region process data
  
 function loadPower($id){
     global $objDbSelect;
     $objResponse = new xajaxResponse();
     $lang = isset($_SESSION['lang'])?$_SESSION['lang']:'vi';
     $cbb = createCombobox('tbl_power',array('id',"power_name_$lang"),array('value'=>'id','name' =>"power_name_$lang")
     ,'cbbRoles',false," where active=1 and power_type_id=$id" );
     $objResponse->addAssign('dvRoles', 'innerHTML',$cbb);
     return $objResponse->getXML();   
 }
 
 function SaveUser($arrParams){
    global $objDbUpdate;
    $objResponse = new xajaxResponse();    
    $username       =     addslashes(strtolower($arrParams['txtAddUsername']));
    $pass           =     EncryptPass($arrParams['txtPassword']);
    $power_type_id  =     $arrParams['cmbPowerType'];
    $power_id       =     $arrParams['cbbRoles'];
    $email          =     addslashes($arrParams['txtEmail']);
    $city_id        =     $arrParams['cmbCity'];
    $address        =     addslashes($arrParams['txtAddress']);
    $phone          =     addslashes($arrParams['txtPhone']);
    $active         =     $arrParams['chkActive'];
    
    $sql = "insert into tbl_user(username,pass,power_type_id,power_id,email,city_id,address,phone,active)
    value('$username','$pass',$power_type_id,$power_id,'$email',$city_id,'$address','$phone',$active)";
    if(!$objDbUpdate->Execute($sql)){
        $objResponse->addAlert('Luu that bai');
    }
    $objResponse->addScript('xajax_ajxLoadDataUser();');
    return $objResponse->getXML(); 
 }
 
function ajxLoadDataUser($arrParams=array())
{     
    global $lang,$objDbSelect;
    $power_type = isset($_SESSION['current']['power_type'])?$_SESSION['current']['power_type']:4;
    $lang = isset($_SESSION['lang'])?$_SESSION['lang']:'vi';
    $arrUser = GetUserGrid($arrParams);
    $objResponse = new xajaxResponse();
    $oTbs = new clsTinyButStrong();
    $tmplGrid = 'templates/'.$lang."/main_users_grid.html"; 
    if(!file_exists($tmplGrid)){
      $objResponse->addAlert('Khong co quyen truy cap chuc nang nay');
      $objResponse->addRedirect('index.php?m=home');  
      return $objResponse->getXML();
    }
    $oTbs->LoadTemplate($tmplGrid);
    $oTbs->MergeBlock('blk_data', $arrUser);
    $oTbs->Show(TBS_NOTHING);    
    $objResponse->addAssign('divGrid', 'innerHTML', $oTbs->Source);   
    $objResponse->addScriptCall('initTable();');                              
    return $objResponse->getXML();
}

function  getHotelByAgent(){
    global $objDbSelect;
    $user_id = isset($_SESSION['current']['user_id'])?$_SESSION['current']['user_id']:2;
    $sql ="SELECT id,hotel_name FROM tbl_service where user_create = $user_id";
    $arr = $objDbSelect->GetArray($sql);
    return $arr;
}

function GetUserGrid($arrParams= array()){
    global $objDbSelect;
    $lang = isset($_SESSION['lang'])?$_SESSION['lang']:'vi';
    $condition = '';
    $sql= "select u.id,u.active,username,pt.name_$lang as power_type,p.power_name_$lang as power ,u.address,u.phone,u.email from 
    tbl_user u join tbl_power_type as pt on pt.power_type_id = u.power_type_id join tbl_power  as p on  p.id = u.power_id  ";
   // echo $sql; die();
    $arr = $objDbSelect->GetArray($sql);
    return $arr;
}

?>
